package com.kgc.cgprovider.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.cgapi.pojo.*;
import com.kgc.cgprovider.dao.CGBJDao;
import com.kgc.cgprovider.dao.CGDao;
import com.kgc.cgprovider.service.CGBJService;
import com.kgc.cgprovider.service.CGServ;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class CGBJServiceImpl implements CGBJService {

    private Logger logger = LoggerFactory.getLogger(CGBJServiceImpl.class);

    @Resource
    private CGBJDao cgbjDao;

    @Resource
    private CGServ cgServ;


    @Override
    public ResponseMsg getCGBJPage(Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        int pageNow = 0;
        int pageSize = 0;
        if(map.get("pageNum") instanceof String){
            pageNow =Integer.parseInt((String) map.get("pageNum"));
            pageSize =Integer.parseInt((String) map.get("pageSize"));
        }else {
            pageNow = (Integer) map.get("pageNum");
            pageSize = (Integer) map.get("pageSize");
        }
        PageHelper.startPage(pageNow,pageSize);
        List<CGBJPojo> list = cgbjDao.getCGBJPage(map);
        PageInfo<CGBJPojo> pageInfo = new PageInfo<>(list);
        responseMsg.setCode(200);
        responseMsg.setData(pageInfo);
        return responseMsg;
    }

    @Override
    public ResponseMsg addCGBJ(Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        ArrayList<Map<String, Object>> arr =(ArrayList<Map<String, Object>>) map.get("list");
        String code ="CGBJ"+"-"+ cgServ.getCode("cg_dcgbj");
        map.put("code",code);
        Date date = new Date();
        map.put("create_date",date);
        int result = cgbjDao.addCGBJ(map);
        logger.info("addCGBJ  + map {} result:{}",map,result);
        if (result <= 0){
            return responseMsg;
        }
        for (Map<String,Object> info : arr){
            info.put("gybj_code",info.get("major_code"));
            info.put("major_code",code);
            info.put("create_date",date);
            info.put("create_id",map.get("create_id"));
            result = cgbjDao.addCGBJInfo(info);
            logger.info("addGYBJInfo  + info {} result:{}",info,result);
            if (result <= 0){
                return responseMsg;
            }
        }
        responseMsg.setData(code);
        responseMsg.setCode(200);
        return responseMsg;
    }

    @Override
    public ResponseMsg getCGBJ(String code) {
        ResponseMsg responseMsg = new ResponseMsg();
        CGBJPojo cgbjPojo = cgbjDao.getCGBJ(code);
        logger.info("Get CGBJPOJO {}" , cgbjPojo);
        List<CGBJInfo> list = cgbjDao.getCGBJInfo(code);
        cgbjPojo.setList(list);
        responseMsg.setCode(200);
        responseMsg.setData(cgbjPojo);
        return responseMsg;
    }

    @Override
    public ResponseMsg modiCGBJ(Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        String code = (String) map.get("code");
        Date date = new Date();
        ArrayList<Map<String, Object>> arr =(ArrayList<Map<String, Object>>) map.get("list");
        map.put("modi_date",date);
        int modiCGBJ = cgbjDao.modiCGBJ(map);
        int del = cgbjDao.delCGBJInfo(code);

        logger.info("modiCGBJ result  + map {} result:{} + isDelInfo{}",map,modiCGBJ,del);
        for (Map<String,Object> info : arr){
            info.put("major_code",code);
            info.put("modi_date",date);
            info.put("modi_id",map.get("create_id"));
            info.put("create_date",date);
            info.put("create_id",map.get("create_id"));

            int result = cgbjDao.addCGBJInfo(info);
            logger.info("addCGBJInfo  + info {} result:{}",info,result);
            if (result <= 0){
                return responseMsg;
            }
        }
        responseMsg.setCode(200);
        return responseMsg;
    }

    @Override
    public ResponseMsg getCGBJByProId(Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        int pageNow = 0;
        int pageSize = 0;
        if(map.get("pageNum") instanceof String){
            pageNow =Integer.parseInt((String) map.get("pageNum"));
            pageSize =Integer.parseInt((String) map.get("pageSize"));
        }else {
            pageNow = (Integer) map.get("pageNum");
            pageSize = (Integer) map.get("pageSize");
        }
        PageHelper.startPage(pageNow,pageSize);
        List<String> codeList = cgbjDao.getCGBJCode(map);
        List<CGBJPojo> cgbjList = new ArrayList<>();
        for (String code : codeList){
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("code",code);
            param.put("pro_id",map.get("pro_id"));
            CGBJPojo cgbjPojo = cgbjDao.getCGBJ(code);

            if (!(cgbjPojo.getStatus() >=3 || cgbjPojo.getStatus() <=10)){
                continue;
            }

            List<CGBJInfo> infoList = cgbjDao.getCGXBJInfoByProId(param);
            cgbjPojo.setList(infoList);
            cgbjList.add(cgbjPojo);
        }
        PageInfo<CGBJPojo> pageInfo = new PageInfo<>(cgbjList);
        responseMsg.setCode(200);
        responseMsg.setData(pageInfo);
        return responseMsg;
    }

    @Override
    public ResponseMsg closeCGBJByCGXJ(String xjCode) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        int result = cgbjDao.closeCGBJByCGXJ(xjCode);
        if (result > 0){
            responseMsg.setCode(200);
        }
        return responseMsg;
    }
}
